package com.example.zhou.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.graphics.Palette;
import android.support.v7.graphics.Target;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.zhou.androidmusic.AlbumInfoActivity;
import com.example.zhou.androidmusic.R;
import com.example.zhou.filehelper.AlbumUtil;
import com.example.zhou.musicmodel.MusicBean;
import com.example.zhou.musicmodel.PalleteColor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 11065 on 2016/11/16.
 */

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
    private List<Map<String, List<MusicBean>>> lists = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private List<String> ablumList = new ArrayList<>();
    private List<PalleteColor> palleteColors = new ArrayList<>();

    private OnItemClick onItemClick1;

    public AlbumAdapter(Context context, List<Map<String, List<MusicBean>>> lists, List<String> ablumList,
                        List<PalleteColor> palleteColors) {
        this.lists = lists;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.ablumList = ablumList;
        this.palleteColors = palleteColors;
    }

    @Override
    public AlbumAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.album_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, onItemClick);
        return viewHolder;
    }

    /**
     * 点击事件
     */
    private OnItemClick onItemClick = new OnItemClick() {
        @Override
        public void OnClick(View view, List<MusicBean> musicBeen, int postion) {
            onItemClick1.OnClick(view, musicBeen, postion);
        }
    };

    public void setOnItemClick(OnItemClick onItemClick1) {
        this.onItemClick1 = onItemClick1;
    }

    @Override
    public void onBindViewHolder(final AlbumAdapter.ViewHolder holder, int position) {
        Map<String, List<MusicBean>> list = lists.get(position);
        String key = ablumList.get(position);
        MusicBean musicBean = list.get(key).get(0);
        List<MusicBean> beanList = list.get(key);
        byte[] bytes = AlbumUtil.getMusicAlbum(new File(musicBean.getFilepath()));

        Glide.with(context).load(bytes).asBitmap().dontAnimate().error(R.drawable.ic_music).into(holder.iv_album);

        Glide.with(context).load(bytes).asBitmap().into(new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                Palette.from(resource).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {
                        Palette.Swatch vibrant =
                                palette.getDarkVibrantSwatch();
                        if (vibrant != null) {
                            // If we have a vibrant color
                            // update the title TextView
                            int rgb = vibrant.getRgb();
                            int bodyTextColor = vibrant.getBodyTextColor();
                            holder.linearLayout.setBackgroundColor(
                                    rgb);
                            holder.tv_album.setTextColor(
                                    bodyTextColor);
                            holder.tv_song_count.setTextColor(
                                    bodyTextColor);
                            holder.tv_songer.setTextColor(
                                    bodyTextColor);
                          /*  holder.frameLayout.setBackgroundColor(rgb);*/
                        }
                    }
                });
            }
        });

        holder.tv_album.setText(key);
        holder.tv_songer.setText(musicBean.getArtist());
        holder.tv_song_count.setText(list.get(key).size() + "歌曲");
        holder.bindView(beanList);


    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.iv_album)
        ImageView iv_album;
        @BindView(R.id.tv_songer)
        TextView tv_songer;
        @BindView(R.id.tv_song_count)
        TextView tv_song_count;
        @BindView(R.id.tv_album)
        TextView tv_album;
        @BindView(R.id.linear)
        LinearLayout linearLayout;
        @BindView(R.id.frameLayout)
        FrameLayout frameLayout;
        private OnItemClick onItemClick;

        public ViewHolder(View itemView, OnItemClick onItemClick) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.onItemClick = onItemClick;
            frameLayout.setOnClickListener(this);
            this.onItemClick = onItemClick;
        }

        public void bindView(List<MusicBean> list) {
            frameLayout.setTag(list);
        }

        @Override
        public void onClick(View view) {
            onItemClick.OnClick(view, (List<MusicBean>) view.getTag(), getLayoutPosition());
        }
    }

    public interface OnItemClick {
        void OnClick(View view, List<MusicBean> musicBeen, int postion);
    }
}
